package service

import (
	"context"
	"github.com/gogf/gf/v2/frame/g"
	"lot_admin_api/api"
	"lot_admin_api/internal/consts"
	"lot_admin_api/internal/dao"
	"lot_admin_api/internal/model"
	GenInt64Id "lot_admin_api/utility/genInt64IdOfString"
	"lot_admin_api/utility/paging"
	"lot_admin_api/utility/response"
)

type sCoupon struct {
}

var insCoupon = sCoupon{}

func Coupon() *sCoupon {
	return &insCoupon
}

// AddCoupon 添加优惠券
func (s *sCoupon) AddCoupon(ctx context.Context, req *api.AddCouponReq) (lastInsertId string, err error) {
	req.Id = GenInt64Id.GenInt64IdOfString() // 生成雪花id
	r := g.RequestFromCtx(ctx)
	// 判断code 是不是为唯一值
	if req.CouponCode != "" {
		codeInfo := g.Map{
			"coupon_code": req.CouponCode, // 优惠券的code
		}
		if findOne, _ := dao.LotCoupon.Ctx(ctx).Where(codeInfo).One(); findOne != nil {
			response.JsonExit(r, consts.HTTPInternalServer.Code(), "优惠券别名已被使用")
		}
	}
	if _, err := dao.LotCoupon.Ctx(ctx).OmitEmpty().Save(req); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "保存数据失败")
	}
	return req.Id, err
}

// UpdateCoupon 更新优惠券
func (s *sCoupon) UpdateCoupon(ctx context.Context, req *api.UpdateCouponReq) (lastInsertId string, err error) {
	r := g.RequestFromCtx(ctx)
	if _, err = s.SearchCoupon(ctx, req.Id); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	if _, err = dao.LotCoupon.Ctx(ctx).OmitEmpty().Where("id", req.Id).Update(&req); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "更新失败")
	}
	return req.Id, err
}

// DeleteCoupon  删除优惠券
func (s *sCoupon) DeleteCoupon(ctx context.Context, req *api.DeleteCouponReq) (lastInsertId string, err error) {
	r := g.RequestFromCtx(ctx)
	if _, err = s.SearchCoupon(ctx, req.Id); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	if _, err = dao.LotCoupon.Ctx(ctx).Where("id", req.Id).Delete(); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "删除失败")
	}
	return req.Id, err
}

// CouponList 获取优惠券列表
func (s *sCoupon) CouponList(ctx context.Context, req *model.CouponListReq) (res *model.CouponListPageRes, err error) {
	var couponList = make([]*model.CouponIdAndInfoRes, 0)
	startNum, endNum := paging.Paging(req.Paging)
	condition := g.Map{
		"coupon_name like ?": "%" + req.CouponName + "%", // 模糊优惠券名字
		"coupon_type":        req.CouponType,             // 优惠券类型
		"term_validity":      req.TermValidity,           // 有效期类型
	}
	r := g.RequestFromCtx(ctx)
	// 获取优惠券列表
	if err = dao.LotCoupon.Ctx(ctx).OmitEmpty().Where(condition).Limit(startNum, endNum).OrderDesc("created_at").Scan(&couponList); err != nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), err.Error())
	}
	// 获取总条数
	countInfo, err := dao.LotCoupon.Ctx(ctx).OmitEmpty().Where(condition).Count()
	putCount := paging.PutCount(req.Paging, countInfo)
	res = &model.CouponListPageRes{
		List:    couponList,
		PutPage: *putCount,
	}
	return res, err
}

// SearchCoupon 判断有没有优惠券
func (s *sCoupon) SearchCoupon(ctx context.Context, Id string) (findOne *model.CouponIdAndInfoRes, err error) {
	r := g.RequestFromCtx(ctx)
	searchInfo := g.Map{
		"id": Id, // 优惠券类型
	}
	err = dao.LotCoupon.Ctx(ctx).Where(searchInfo).Scan(&findOne)
	if findOne == nil {
		response.JsonExit(r, consts.HTTPInternalServer.Code(), "没有该优惠券")
	}
	return findOne, err
}
